/**
 * 
 */
package br.com.ctm.guiamedico.rest.impl;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.PersistenceException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.annotations.providers.jaxb.Wrapped;

import br.com.ctm.guiamedico.model.Especialidade;
import br.com.ctm.guiamedico.model.Medico;
import br.com.ctm.guiamedico.negocio.api.MedicoNegocio;
import br.com.framework.negocio.exception.NegocioException;

/**
 * @author Cleber
 * 
 */
@Path("medicos")
@Named
public class MedicoResource extends EntityResourceImpl<Medico, MedicoNegocio> {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.ctm.guiamedico.rest.api.EntityResourceImpl#setNegocio(br.com.framework
	 * .negocio.api.BaseNegocio)
	 */
	@Override
	@Inject
	protected void setNegocio(MedicoNegocio negocio) {
		super.setNegocio(negocio);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.ctm.guiamedico.rest.api.EntityResource#findByID(java.lang.Long)
	 */
	@Override
	@GET
	@Path("{id}")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Medico findByID(@PathParam("id") Long id)
			throws PersistenceException {
		return super.findByID(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.ctm.guiamedico.rest.api.EntityResource#findAll()
	 */
	@Override
	@GET
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Wrapped(element = "medicos")
	public List<Medico> findAll() throws PersistenceException {
		return super.findAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.ctm.guiamedico.rest.api.EntityResource#findAll(int, int)
	 */
	@Override
	@GET
	@Path("paginated/{firstResult}/{maxResults}")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Wrapped(element = "medicos")
	public List<Medico> findAll(@PathParam("firstResult") int firstResult,
			@PathParam("maxResults") int maxResults)
			throws PersistenceException {
		return super.findAll(firstResult, maxResults);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.ctm.guiamedico.rest.api.EntityResource#findByExample(br.com.framework
	 * .persistencia.model.BaseEntity)
	 */
	@Override
	@GET
	@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Wrapped(element = "medicos")
	public List<Medico> findByExample(Medico entidade)
			throws PersistenceException {
		return super.findByExample(entidade);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.ctm.guiamedico.rest.api.EntityResource#getRecordCount()
	 */
	@Override
	@GET
	@Path("count")
	@Produces(MediaType.TEXT_PLAIN)
	public Long getRecordCount() throws PersistenceException {
		return super.getRecordCount();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * br.com.ctm.guiamedico.rest.api.EntityResource#insert(br.com.framework
	 * .persistencia.model.BaseEntity)
	 */
	@Override
	@POST
	@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Response insert(Medico entidade) throws NegocioException,
			PersistenceException {
		return super.insert(entidade);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.ctm.guiamedico.rest.api.EntityResource#update(java.lang.Long,
	 * br.com.framework.persistencia.model.BaseEntity)
	 */
	@Override
	@PUT
	@Path("{id}")
	@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Response update(@PathParam("id") Long id, Medico entidade)
			throws NegocioException, PersistenceException {
		return super.update(id, entidade);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.ctm.guiamedico.rest.api.EntityResource#remove(java.lang.Long)
	 */
	@Override
	@DELETE
	@Path("{id}")
	public Response remove(@PathParam("id") Long id) throws NegocioException,
			PersistenceException {
		return super.remove(id);
	}

	/**
	 * @param id
	 * @return
	 */
	@GET
	@Path("{id}/especialidades")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Wrapped(element = "especialidades")
	public List<Especialidade> getEspecialidades(@PathParam("id") Long id) {
		Medico medico = findByID(id);
		List<Especialidade> result = new ArrayList<Especialidade>();
		if (medico != null) {
			result.addAll(medico.getEspecialidades());
		}
		return result;
	}

	@GET
	@Path("inseridos/{numeroVersao}/count")
	@Produces(MediaType.TEXT_PLAIN)
	public Long getCountMedicosIncluidosNaVersao(
			@PathParam("numeroVersao") Long numeroVersao)
			throws PersistenceException {
		return getNegocio().getCountMedicosIncluidosNaVersao(numeroVersao);
	}

	@GET
	@Path("inseridos/{numeroVersao}/{firstResult}/{maxResults}")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Wrapped(element = "medicos")
	public List<Medico> getMedicosIncluidosNaVersao(
			@PathParam("numeroVersao") Long numeroVersao,
			@PathParam("firstResult") int firstResult,
			@PathParam("maxResults") int maxResults)
			throws PersistenceException {
		return getNegocio().getMedicosIncluidosNaVersao(numeroVersao,
				firstResult, maxResults);
	}
	
	@GET
	@Path("removidos/{numeroVersao}/count")
	@Produces(MediaType.TEXT_PLAIN)
	public Long getCountMedicosRemovidosNaVersao(
			@PathParam("numeroVersao") Long numeroVersao)
			throws PersistenceException {
		return getNegocio().getCountMedicosRemovidosNaVersao(numeroVersao);
	}

	@GET
	@Path("removidos/{numeroVersao}/{firstResult}/{maxResults}")
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	@Wrapped(element = "medicos")
	public List<Medico> getMedicosRemovidosNaVersao(
			@PathParam("numeroVersao") Long numeroVersao,
			@PathParam("firstResult") int firstResult,
			@PathParam("maxResults") int maxResults)
			throws PersistenceException {
		return getNegocio().getMedicosRemovidosNaVersao(numeroVersao,
				firstResult, maxResults);
	}
}
